<?php

// 本类由系统自动生成，仅供测试用途

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

    public function index() {
        $data = U('index/sign', array('mid' => 2));
        //查询所有可签到会议列表
        $Meeting = M('meeting'); // 实例化User对象
        $meetings = $Meeting->where('sts=1')->select();
        $this->assign('meetings', $meetings);
        $this->show();
    }

    public function dologin() {
        session('logined_email', I('post.email'));  //设置session
        $this->redirect('index');
    }

    public function add() {
        $this->checklogin();
        $this->show();
    }

    public function doadd() {
        $this->checklogin();
        $Meeting = M('meeting'); // 实例化User对象
        $ok = $Meeting->create();
        if ($ok) {
            $Meeting->email = session('logined_email');
            $result = $Meeting->add(); // 写入数据到数据库
            if ($result) {
                // 如果主键是自动增长型 成功后返回值就是最新插入的值 
            } else {
                $ok = false;
            }
        }

        if ($ok) {
            $this->success('发布成功', U('index/index'));
        } else {
            $this->error('发布失败');
        }
    }

    private function checklogin() {
        if (!session('logined_email'))
            $this->redirect('login');
    }

    public function qrcode() {
        $meetingid = I('get.mid');
        // 二维码数据
        $data = U('index/sign', array('mid' => $meetingid), 'jsp', true);
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 8;
        // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
        //$path = "images/";
        // 生成的文件名
        //$fileName = $path.$size.'.png';
        \Org\Util\QRcode::png($data, false, $level, $size);
    }

    public function sign() {
        $meetingid = I('get.mid');
        $this->assign('mid', $meetingid);
        $this->show();
    }

    public function dosign() {
        $data['mid'] = I('post.mid');
        $data['mobile'] = I('post.mobile');
        if (!$data['mid']) {
            $this->error('没有这个会议哦:(');
        }
        if (!$data['mid'] || strlen($data['mobile']) != 11) {
            $this->error('手机号有误');
        }
        $signed = M('signed'); // 实例化User对象 
        $already_signed = $signed->where($data)->select();
        $ok = false;
        $data['lasttime'] = time();
        if (!$already_signed) {
            $ok = $signed->add($data);
        } else {
            $ok = $signed->save($data);
        }
        if ($ok) {
            $this->success('签到成功，您会出现在大屏幕', U('index/index'));
        } else {
            $this->error('签到成功');
        }
    }

    public function detail() {
        $meetingid = I('get.mid');
        $pdata['meeting'] = M('Meeting')->find($meetingid);
        $pdata['signed'] = D('Signed')->getSigned($meetingid);
        $this->assign($pdata);
        $this->show();
    }

    public function del() {
        $condition['id'] = I('get.mid');
        $ok = M("Meeting")->where($condition)->setField('sts', 0);
        if (false == $ok || 0 == $ok) {
            $this->error('删除失败');
        } else {
            $this->success('会议删除了', U('index/index'));
        }
    }

}